import request from './request'

// 获取文章列表
export function getArticlesList(params = {}) {
  return request({
    url: '/articles/list',
    method: 'get',
    params: {
      page: params.page || 1,
      pageSize: params.pageSize || 12,
      categoryId: params.categoryId || null,
      search: params.search || '',
      sortBy: params.sortBy || 'latest',
      ...params
    }
  })
}

// 获取文章详情
export function getArticleDetail(id) {
  return request({
    url: `/articles/${id}`,
    method: 'get'
  })
}

// 获取热门文章
export function getPopularArticles(limit = 10) {
  return request({
    url: '/articles/popular',
    method: 'get',
    params: { limit }
  })
}

// 获取推荐文章
export function getRecommendedArticles(categoryId = null, limit = 10) {
  return request({
    url: '/articles/recommended',
    method: 'get',
    params: { categoryId, limit }
  })
}

// 点赞文章
export function likeArticle(id) {
  return request({
    url: `/articles/${id}/like`,
    method: 'post'
  })
}

// 取消点赞文章
export function unlikeArticle(id) {
  return request({
    url: `/articles/${id}/unlike`,
    method: 'post'
  })
}

// 收藏文章
export function bookmarkArticle(id) {
  return request({
    url: `/articles/${id}/bookmark`,
    method: 'post'
  })
}

// 取消收藏文章
export function unbookmarkArticle(id) {
  return request({
    url: `/articles/${id}/unbookmark`,
    method: 'post'
  })
}

// 增加文章阅读量
export function incrementArticleViews(id) {
  return request({
    url: `/articles/${id}/view`,
    method: 'post'
  })
}

// 获取用户收藏的文章
export function getUserBookmarks(params = {}) {
  return request({
    url: '/articles/bookmarks',
    method: 'get',
    params: {
      page: params.page || 1,
      pageSize: params.pageSize || 12,
      ...params
    }
  })
}

// 获取用户点赞的文章
export function getUserLikes(params = {}) {
  return request({
    url: '/articles/likes',
    method: 'get',
    params: {
      page: params.page || 1,
      pageSize: params.pageSize || 12,
      ...params
    }
  })
}

// 搜索文章
export function searchArticles(query, params = {}) {
  return request({
    url: '/articles/search',
    method: 'get',
    params: {
      q: query,
      page: params.page || 1,
      pageSize: params.pageSize || 12,
      categoryId: params.categoryId || null,
      sortBy: params.sortBy || 'relevance',
      ...params
    }
  })
}

// 获取文章统计信息
export function getArticleStats(id) {
  return request({
    url: `/articles/${id}/stats`,
    method: 'get'
  })
}

// 获取相关文章
export function getRelatedArticles(id, limit = 5) {
  return request({
    url: `/articles/${id}/related`,
    method: 'get',
    params: { limit }
  })
}
